﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

/// <summary>
/// 获取订单[外卖订单]
/// </summary>
public partial class AndroidAPI_Deliver_GetOrderListByUserId : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        Custorder dal = new Custorder();
      

        int pagesize = HjNetHelper.GetPostParam("pagesize", 8);
        int pageindex = HjNetHelper.GetPostParam("pageindex", 1);
        int pagecount = 1;

        string order_state = WebUtility.InputText(Request["state"]);//订单状态
        int did = HjNetHelper.GetPostParam("did", 0); ;//配送员编号
        int cityid = HjNetHelper.GetPostParam("cityid", 0); ;//城市编号（发群时要判断）

        StringBuilder orderlistjson = new StringBuilder();

        IList<CustorderInfo> list = new List<CustorderInfo>();

        string SqlWhere = " 1=1 and Custorder.delflag= 0 and OrderStatus > 2  ";

        //群编号
        int gid = HjNetHelper.GetPostParam("gid", 0);
        if (gid > 0)
        {
            SqlWhere += " and  (deliverheaderid=" + gid + ") and OrderStatus=7 and sendstate = 0   ";
            if (cityid > 0)
            {
                SqlWhere += " and  custorder.cityid =" + cityid;
            }
        }
        if (did != 0)
        {
            SqlWhere += " and  custorder.deliverid=" + did;
        }

        if (order_state != null && order_state != "")
        {
            if (order_state != "-1")//全部订单
            {
                SqlWhere += " and  OrderStatus=" + order_state;
            }
        }

        string searchkey = WebUtility.InputText(Request["searchkey"]);//订单编号，或者电话
        if (searchkey != null && searchkey.Length > 0)
        {
            SqlWhere += " and ( custorder.orderid like '%" + searchkey + "%' or OrderComm like '%" + searchkey + "%') ";
        }

        int count = dal.DeliverGetOrderCount(SqlWhere);

        list = dal.DeliverGetOrderList(pagesize, pageindex, SqlWhere, "sendstate asc, Custorder.Unid", 1);//1 降序

        if (count % pagesize == 0)//整数倍
        {
            pagecount = count / pagesize;
        }
        else
        {
            pagecount = count / pagesize + 1;
        }

        orderlistjson.Append("{\"page\":\"" + pageindex.ToString() + "\",\"total\":\"" + pagecount + "\", \"orderlist\":[");

        CustorderInfo info = new CustorderInfo();
        for (int i = 0; i < list.Count; i++)
        {
            info = new CustorderInfo();
            info = list[i];
            orderlistjson.Append("{\"orderid\":\"" + info.orderid + "\"");
            orderlistjson.Append(",\"shopname\":\"" + info.TogoName + "\"");
            orderlistjson.Append(",\"addtime\":\"" + info.OrderDateTime.ToString("yyyy-MM-dd HH:mm:dd") + "\"");
            orderlistjson.Append(",\"totalmoney\":\"" + info.OrderSums.ToString("0.0") + "\"");
            orderlistjson.Append(",\"state\":\"" + info.OrderStatus.ToString() + "\"");
            orderlistjson.Append(",\"address\":\"" + info.AddressText.ToString() + "\"");
            orderlistjson.Append(",\"PayMode\":\"" + info.paymode + "\"");

            orderlistjson.Append(",\"sendstate\":\"" + info.sendstate + "\"");

            orderlistjson.Append(",\"isupdate\":\"" + info.Sender + "\"");

            orderlistjson.Append(",\"IsShopSet\":\"" + info.IsShopSet + "\"");
            orderlistjson.Append(",\"SystemUserId\":\"" + info.SystemUserId + "\"");
            orderlistjson.Append(",\"paystate\":\"" + info.paystate + "\"");
            orderlistjson.Append(",\"sendmoney\":\"" + info.DeliverGetSendFee + "\"");
            orderlistjson.Append(",\"realname\":\"" + info.OrderRcver + "\"");
            orderlistjson.Append(",\"phone\":\"" + info.OrderComm + "\"");

            int ordertype = 1;
            if (info.TogoId == 0)
            {
                ordertype = 2;
            }

            orderlistjson.Append(",\"ordertype\":\""+ ordertype + "\"");
            orderlistjson.Append("},");
        }


        orderlistjson.Append("]}");

        Response.Write(orderlistjson.ToString().Replace("},]}", "}]}"));
        Response.End();


    }
}
